package test;

public class thread_sy implements Runnable {
	 Thread t;
	 String name;
	 String data;
	 private Object obj;
	 /**
	  * obj from main,so different thread can share the same obj.
	  * if obj=new Object() here in thread_sy class,each thread will create a new obj,so sychronize
	  * is invalid
	  */
	 
	 public thread_sy(String name,String data,Object obj){
		 this.name=name;
		 this.data=data;
		 this.obj=obj;
		 
	 }
	 public  void run(){
		 while(true){
			synchronized (this.obj) {
			
				try{
					//this.obj.wait();
					 System.out.print(this.data+"1");
					 t.sleep(10);
					 System.out.println(this.data+"2");
					 //this.obj.notifyAll();
					 //
				 }catch( Exception e){
					 e.printStackTrace();
				 }
				//
			}	
					 
			
		 }
		 
	 }
	 public void start(){
		 if(t==null){
			 t=new Thread(this,this.name);
			 t.start();
		 }
	 }

}
